package com.example.skillcinema.ui.screens.search

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.core.widget.addTextChangedListener
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import com.example.skillcinema.R
import com.example.skillcinema.adapters.FilterRecyclerViewAdapter
import com.example.skillcinema.databinding.FragmentCountryAndGenreSearchBinding
import com.example.skillcinema.db.displaymodels.FilterForDisplay
import com.example.skillcinema.db.responses.FiltersCountriesResponse
import com.example.skillcinema.db.responses.FiltersGenresResponse
import com.example.skillcinema.ui.screens.search.viewModels.SearchViewModel
import kotlinx.coroutines.launch

class CountryAndGenreSearchFragment : Fragment() {
    private var _binding: FragmentCountryAndGenreSearchBinding? = null
    private val binding: FragmentCountryAndGenreSearchBinding
        get() = _binding!!
    private val viewModel: SearchViewModel by activityViewModels<SearchViewModel>()
    private var countryOrGenre: Boolean = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            countryOrGenre = it.getBoolean(ARG_PARAM1)
        }
    }

    private var list: List<FilterForDisplay> = listOf()
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        _binding = FragmentCountryAndGenreSearchBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        (activity as? AppCompatActivity)?.setSupportActionBar(binding.toolbar)
        (activity as? AppCompatActivity)?.supportActionBar?.setDisplayHomeAsUpEnabled(true)
        (activity as? AppCompatActivity)?.supportActionBar?.setDisplayShowTitleEnabled(false)
        binding.toolbarTitle.text = if (countryOrGenre) "Country" else "Genre"
        binding.toolbar.setNavigationOnClickListener {
            findNavController().navigateUp()
        }
        val adapter: FilterRecyclerViewAdapter =
            FilterRecyclerViewAdapter { id, name -> select(id, name) }
        binding.resultRecyclerView.adapter = adapter
        binding.searchEditText.addTextChangedListener { text ->
            if (text.toString() == "") {
                adapter.setData(list)
            } else {
                adapter.setData(listOf(list.find { it.name == text.toString().trim() }
                    ?: FilterForDisplay(-1, "No data found")))
            }
        }
        lifecycleScope.launch {
            if (!countryOrGenre) {
                viewModel.countries.collect {
                    list = it
                    adapter.setData(it)
                }
            } else {
                viewModel.genres.collect {
                    list = it
                    adapter.setData(it)
                }
            }
        }
    }

    private fun select(id: Int, name: String) {
        if (id > 0)
            if (!countryOrGenre) {
                viewModel.country = FiltersCountriesResponse(id, name)
            } else {
                viewModel.genre = FiltersGenresResponse(id, name)
            }
    }

    companion object {
        @JvmStatic
        fun newInstance(param1: Boolean) =
            CountryAndGenreSearchFragment().apply {
                arguments = Bundle().apply {
                    putBoolean(ARG_PARAM1, param1)
                }
            }

        const val ARG_PARAM1 = "county_or_genre"
    }
}